package swimv2gm.client.admin;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import swimv2gm.bl.domain.*;
import swimv2gm.bl.session.AbilityManager;

/**
 * Prepara i dati di una competenza per la modifica da parte di un
 * amministratore.
 * 
 * @author Gallesio Matteo
 */
public class EditAbility extends HttpServlet {

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		AbilityManager man;
		try {
			man = (AbilityManager)InitialContext.doLookup("AbilityManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}

		Boolean isAdmin = (Boolean)request.getSession().getAttribute("admin");
		if (isAdmin == null || !isAdmin) {
			response.sendError(403);
			return;
		}
		String nextpage;
		long cid = -1;
		try {
			cid = Long.parseLong(request.getParameter("cid"));
		} catch (NumberFormatException e) {
			nextpage = "addAbility.jsp?cid=-1";
		}
		Ability a = man.getAbility(cid);
		if (a != null) {
			request.setAttribute("nome",a.getNome());
			request.setAttribute("descrizione",a.getDescrizione());
		}
		else {
			nextpage = "addAbility.jsp?cid=-1";
		}
		nextpage = "addAbility.jsp?action=edit";

		request.getRequestDispatcher(nextpage).forward(request,response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
